package com.zzy.banian.crm.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.zzy.banian.crm.domain.Lead;
import com.zzy.banian.crm.service.ILeadService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 线索信息操作处理
 * 
 * @author zhangzuoyi
 * @date 2019-05-05
 */
@Controller
@RequestMapping("/crm/lead")
public class LeadController extends BaseController
{
    private String prefix = "crm/lead";
	
	@Autowired
	private ILeadService leadService;
	
	@RequiresPermissions("crm:lead:view")
	@GetMapping()
	public String lead()
	{
	    return prefix + "/lead";
	}
	
	/**
	 * 查询线索列表
	 */
	@RequiresPermissions("crm:lead:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Lead lead)
	{
		startPage();
        List<Lead> list = leadService.selectLeadList(lead);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出线索列表
	 */
	@RequiresPermissions("crm:lead:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Lead lead)
    {
    	List<Lead> list = leadService.selectLeadList(lead);
        ExcelUtil<Lead> util = new ExcelUtil<Lead>(Lead.class);
        return util.exportExcel(list, "lead");
    }
	
	/**
	 * 新增线索
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存线索
	 */
	@RequiresPermissions("crm:lead:add")
	@Log(title = "线索", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Lead lead)
	{		
		return toAjax(leadService.insertLead(lead));
	}

	/**
	 * 修改线索
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Lead lead = leadService.selectLeadById(id);
		mmap.put("lead", lead);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存线索
	 */
	@RequiresPermissions("crm:lead:edit")
	@Log(title = "线索", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Lead lead)
	{		
		return toAjax(leadService.updateLead(lead));
	}
	
	/**
	 * 删除线索
	 */
	@RequiresPermissions("crm:lead:remove")
	@Log(title = "线索", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(leadService.deleteLeadByIds(ids));
	}
	
}
